package com.wy.xinzhi.common.actions;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.component.properties.PropertiesUtil;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.exceptions.ServiceException;
import com.wy.xinzhi.common.pojos.TStore;
import com.wy.xinzhi.common.services.StoreService;

/**
 * 门店管理
 * 
 * @author X
 * 
 */
@ParentPackage("json")
@Namespace(value = "/xz")
@SuppressWarnings({ "rawtypes", "unused" })
public class StoreAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(StoreAction.class);
	private PropertiesUtil p = new PropertiesUtil();

	// 是否成功
	private String jsonRes;
	// 门店列表
	private List<Map<String, Object>> stores;

	@Autowired
	private StoreService storeService;

	/**
	 * 获取门店信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@Action(value = "queryStores", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String queryStores() throws UnsupportedEncodingException {
		stores = storeService.queryStores();
		if (stores != null) {
			this.jsonRes = "true";
		} else {
			this.jsonRes = "false";
		}
		return SUCCESS;
	}

	/**
	 * 新增门店信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ServiceException
	 */
	@Action(value = "addStores", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String addStores() throws UnsupportedEncodingException,
			ServiceException {
		String name = ServletActionContext.getRequest().getParameter("name");
		String address = ServletActionContext.getRequest().getParameter(
				"address");
		String phone = ServletActionContext.getRequest().getParameter("phone");
		TStore store = new TStore();

		store.setName(name);
		store.setAddress(address);
		store.setPhone(phone);
		store.setCreateDate(new Timestamp(new Date().getTime()));
		// 需要确定获取用户信息后修改
		store.setUserId(1);
		storeService.addStore(store);
		return SUCCESS;
	}

	/**
	 * 删除门店信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@Action(value = "deleteStores", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String deleteStores() throws UnsupportedEncodingException {
		String storeId = ServletActionContext.getRequest().getParameter(
				"storeId");
		TStore store = new TStore();
		store.setStoreId(Integer.parseInt(storeId));
		storeService.deleteStore(store);
		return SUCCESS;
	}

	/**
	 * 修改门店信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws ServiceException
	 */
	@Action(value = "updateStores", results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false" }) })
	public String updateStores() throws UnsupportedEncodingException,
			ServiceException {
		String storeId = ServletActionContext.getRequest().getParameter(
				"storeId");
		String name = ServletActionContext.getRequest().getParameter("name");
		String address = ServletActionContext.getRequest().getParameter(
				"address");
		String phone = ServletActionContext.getRequest().getParameter("phone");
		TStore store = new TStore();
		store.setStoreId(Integer.parseInt(storeId));
		store.setName(name);
		store.setAddress(address);
		store.setPhone(phone);
		store.setCreateDate(new Timestamp(new Date().getTime()));
		// 需要确定获取用户信息后修改
		store.setUserId(1);
		storeService.updateStore(store);
		return SUCCESS;
	}

	public String getJsonRes() {
		return jsonRes;
	}

	public void setJsonRes(String jsonRes) {
		this.jsonRes = jsonRes;
	}

	public List<Map<String, Object>> getStores() {
		return stores;
	}

	public void setStores(List<Map<String, Object>> stores) {
		this.stores = stores;
	}

}
